home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / ams__l~1.zoo / src / fnameext.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-25  |  2.0 KB  |  95 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is part of the Atari Machine Specific Library,
  4. //  and is Copyright 1992 by Warwick W. Allison.
  5. //
  6. //  You are free to copy and modify these sources, provided you acknowledge
  7. //  the origin by retaining this notice, and adhere to the conditions
  8. //  described in the file COPYING.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #include "fnameext.h"
  12.  
  13. #define FILESEP '/'
  14. #define EXTSEP '.'
  15.  
  16. char* basename(char* dest,const char* src)
  17. {
  18.     // To end
  19.     for (int i=0; src[i]; i++);
  20.  
  21.     // Back to start or after FILESEP
  22.     while (i && src[i-1]!=FILESEP) i--;
  23.  
  24.     const char* result=&src[i];  // default = end of src
  25.  
  26.     // Copy from there to end
  27.     for (int j=0; src[i]; i++) dest[j++]=src[i];
  28.  
  29.     // Null terminate
  30.     dest[j]=0;
  31.  
  32.     return (char*)result; // Cast to char* - caller owns src[]
  33. }
  34.  
  35. char* directory(char* dest,const char* src)
  36. {
  37.     // To end
  38.     for (int i=0; src[i]; i++);
  39.  
  40.     // Back to start or after FILESEP
  41.     while (i && src[i-1]!=FILESEP) i--;
  42.  
  43.     // Copy all before there
  44.     for (int j=0; j<i; j++) dest[j]=src[j];
  45.  
  46.     // Null terminate
  47.     dest[j]=0;
  48.  
  49.     return dest;
  50. }
  51.  
  52. char* extension(char* dest,const char* src)
  53. {
  54.     // To end
  55.     for (int i=0; src[i]; i++);
  56.  
  57.     const char* result=&src[i];  // default = end of src
  58.  
  59.     // Back to start, after EXTSEP, or after FILESEP
  60.     while (i && src[i-1]!=FILESEP && src[i-1]!=EXTSEP) i--;
  61.  
  62.     if (i && src[i-1]==EXTSEP) {
  63.         result=&src[i];
  64.  
  65.         // Copy from there to end
  66.         for (int j=0; src[i]; i++) dest[j++]=src[i];
  67.  
  68.         // Null terminate
  69.         dest[j]=0;
  70.     } else {
  71.         // None
  72.         dest[0]=0;
  73.     }
  74.  
  75.     return (char*)result; // Cast to char* - caller owns src[]
  76. }
  77.  
  78. char* noextension(char* dest,const char* src)
  79. {
  80.     // To end
  81.     for (int i=0; src[i]; i++);
  82.  
  83.     // Back to start or after FILESEP
  84.     while (i && src[i-1]!=FILESEP) i--;
  85.  
  86.     // Copy from there to end or before EXTSEP
  87.     for (int j=0; src[i] && src[i]!=EXTSEP; i++) dest[j++]=src[i];
  88.  
  89.     // Null terminate
  90.     dest[j]=0;
  91.  
  92.     return dest;
  93. }
  94.  
  95.